import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:luckiness/utils/general.dart';
import '../components/ffbtn.dart';
import '../utils/network/login_request.dart';
import '../components/ffcountdownbtn.dart';
class Register extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return RegisterState();
  }
}

class RegisterState extends State<Register> {
  Color bgColor = Color.fromRGBO(136, 175, 213, 1.0);
  GlobalKey<FormState> key = GlobalKey<FormState>(); 
  TextEditingController emailCon;
  TextEditingController emsCon;
  TextEditingController passCon;

  @override
  void initState() {
    emailCon = TextEditingController();
    emsCon = TextEditingController();
    passCon = TextEditingController();
    super.initState();
  }

  @override
  void dispose() {
    emailCon.dispose();
    emsCon.dispose();
    super.dispose();
  }

  Widget getUserPortrait() {
    if (FFShareInstance.info.portrait.length > 0) {
      return Image.network(FFShareInstance.info.portrait, width: 80, height: 95, fit:BoxFit.fill);
    }else {
      return Image.asset('assets/images/logo1.png', width:80, height: 95,);
    }
  }

  @override
  Widget build(BuildContext context) {
    double scale = widthScale(context);
    return Scaffold(
      appBar:AppBar(
        backgroundColor: Colors.white,
        leading: FlatButton(
          child: Icon(Icons.keyboard_arrow_left, size:35.0, color:Colors.grey[200]),
          onPressed: ()=>Navigator.of(context).pop()),
      ),
      body: SingleChildScrollView(
        child:Column(
          children: <Widget>[
            Container(
              padding: EdgeInsets.symmetric(vertical:25 * scale),
              child: Center(
                child:getUserPortrait(),
              ),
            ),
            Padding(
              padding: EdgeInsets.symmetric(horizontal:16.0),
              child: Form(
                key:key,
                child: Column(
                    children: <Widget>[
                      TextFormField(
                        controller:emailCon,
                        keyboardType: TextInputType.emailAddress,
                        decoration:InputDecoration(
                          icon: Icon(Icons.email),
                          hintText: "请输入邮箱",
                          border: UnderlineInputBorder(borderSide:BorderSide.none),
                        ),
                        validator: (str){
                          return str.length > 4 ? null : "邮箱有误";
                        },                                                
                      ),
                      TextFormField(
                        controller:emsCon,
                        keyboardType: TextInputType.emailAddress,
                        decoration:InputDecoration(
                          icon: Icon(Icons.message),
                          hintText: "请输入短息验证码",
                          border: UnderlineInputBorder(borderSide:BorderSide.none),
                          suffixIcon: FFCountDownBtn(total:10, height: 20),
                        ),
                        validator: (str){
                          return str.length == 4 ? null : "验证码有误";
                        },                        
                      ),
                      TextFormField(
                        controller:passCon,
                        keyboardType: TextInputType.emailAddress,
                        decoration:InputDecoration(
                          icon: Icon(Icons.lock_outline),
                          hintText: "请输入密码",
                          border: UnderlineInputBorder(borderSide:BorderSide.none),
                        ),                        
                        validator: (par){
                          return (par.length > 3 && par.length <= 8) ? null : "密码有误";
                        },
                        obscureText: true,                                              
                      ),
                    ].map((m){
                      return Column(
                        mainAxisSize:MainAxisSize.min,
                        children:<Widget>[
                          m,
                          Divider(height:0, color:Color.fromRGBO(88, 88, 88, 1.0)),
                        ]
                      );
                    }).toList(),  
                  ),
              ),
            ),
            FFRowBtn(
              title:'确定',
              padding: EdgeInsets.fromLTRB(35 * scale, 20, 35 * scale, 0),
              bgColor:bgColor,
              onPress:(){
                FocusScope.of(context).requestFocus(FocusNode());
                FormState st = key.currentState;
                if (st.validate()) {
                  LoginRequest().loginWith(context: context, email: emailCon.text, sms: emsCon.text, pass: passCon.text);
                }
              }
            ),
            Padding(
              padding: EdgeInsets.fromLTRB(35 * scale, 10, 35 * scale, 0),
              child: Text.rich(
                TextSpan(
                  text:'点击确定即表示阅读并统一',
                  style:TextStyle(fontSize: 12, color:Color.fromRGBO(128, 128, 128, 1.0)),
                  children: [
                    TextSpan(
                      text :'《注册会员服务跳坑》',
                      style:TextStyle(fontSize:12, color:Color.fromRGBO(85, 127, 157, 1.0)),
                      recognizer: TapGestureRecognizer()..onTap=(){
                        print('协议');
                      },
                    )
                  ]
                ),
                textAlign:TextAlign.right,
              ),
            )
          ],
        ),
      ),
    );
  }
}